package org.imlinker.gui;

import java.awt.Frame;
import java.awt.SystemTray;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import org.imlinker.list.ListManager;
import org.imlinker.login.Core;
import org.imlinker.status.StatusManager;
import org.imlinker.util.StringUtils;
import org.jivesoftware.smack.packet.Presence;

@SuppressWarnings("serial")
public class MainWindow extends LocationSavedWindow {

	private JMenuItem addBuddyItem;

	private JMenuItem loginItem;

	private JMenuItem deleteBuddyItem;

	private JMenuItem sayItem;

	private JMenu jMenu2;

	public MainWindow(final Core paraCore, final StatusManager statusManager) {
		super();

		this.core = paraCore;
		initComponents();
		initActions(statusManager);

		// 初始窗口名，位置。
		setName("MainWindow");
		restoreLocation();

	}

	public void setListModel(final ListManager listManager) {
		listManager.initialLists(messageList, buddyList);
		jMenu2.setEnabled(true);
		loginItem.setEnabled(false);
	}

	public void setMessageNum(final int number) {
		listTab.setTitleAt(0, messageTabTitle + "(" + number + ")");
	}

	/**
	 * 处理action，各种按键的反应
	 * @param statusManager
	 */
	private void initActions(final StatusManager statusManager) {

		// 监听关闭窗口事件，如果不支持托盘，则关闭程序
		addWindowListener(new WindowAdapter() {
			public void windowClosing(final WindowEvent e) {
				if (SystemTray.isSupported()) {
					setVisible(false);
				} else {
					System.exit(0);
				}
			}

		});

		quitItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Core.exit(0);
			}

		});

		aboutItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(MainWindow.this, "linker 0.0.2");
			}

		});

		// 好友添加按钮
		addBuddyItem.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				String id = JOptionPane.showInputDialog("Add buddy id:");
				if (StringUtils.isNotEmpty(id) && StringUtils.contains(id, '@')) {
					String from = core.getUser();

					int result = JOptionPane.showConfirmDialog(MainWindow.this,
							"Add buddy " + id + " ?", "Add buddy confirm",
							JOptionPane.OK_CANCEL_OPTION);
					if (result == JOptionPane.CANCEL_OPTION) {
						return;
					}

					Presence presence = new Presence(Presence.Type.subscribe);
					presence.setFrom(from);
					presence.setTo(id);
					core.sendPacket(presence);
				}
			}

		});

		// 删除好友
		deleteBuddyItem.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {

				String id = JOptionPane.showInputDialog("Input delete id");
				if (StringUtils.isEmpty(id)) {
					return;
				}

				String from = core.getUser();

				int result = JOptionPane.showConfirmDialog(MainWindow.this,
						"Delete " + id + " ?", "Delete buddy confirm",
						JOptionPane.OK_CANCEL_OPTION);
				if (result == JOptionPane.CANCEL_OPTION) {
					return;
				}

				Presence presence = new Presence(Presence.Type.unsubscribe);
				presence.setFrom(from);
				presence.setTo(id);
				core.sendPacket(presence);
			}

		});

		sayItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				core.startPlugin("Say");
			}

		});

		loginItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				core.start();
			}

		});
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		listTab = new javax.swing.JTabbedPane();
		JScrollPane messageTab = new javax.swing.JScrollPane();
		messageList = new javax.swing.JList();
		JScrollPane buddyTab = new javax.swing.JScrollPane();
		buddyList = new javax.swing.JList();
		javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
		javax.swing.JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
		javax.swing.JMenu jMenu1 = new javax.swing.JMenu();
		loginItem = new javax.swing.JMenuItem();
		quitItem = new javax.swing.JMenuItem();
		jMenu2 = new javax.swing.JMenu();
		addBuddyItem = new javax.swing.JMenuItem();
		deleteBuddyItem = new javax.swing.JMenuItem();
		sayItem = new javax.swing.JMenuItem();
		javax.swing.JMenu jMenu3 = new javax.swing.JMenu();
		aboutItem = new javax.swing.JMenuItem();

		java.util.ResourceBundle bundle = java.util.ResourceBundle
				.getBundle("org/imlinker/gui/Bundle"); // NOI18N
		setTitle(bundle.getString("MainWindow.title")); // NOI18N

		listTab.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);

		messageTab
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		messageTab.setViewportView(messageList);

		messageTabTitle = bundle
				.getString("MainWindow.messageTab.TabConstraints.tabTitle");
		listTab.addTab(messageTabTitle, messageTab); // NOI18N

		buddyTab
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		buddyTab.setViewportView(buddyList);

		listTab.addTab(bundle
				.getString("MainWindow.buddyTab.TabConstraints.tabTitle"),
				buddyTab); // NOI18N

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING).addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						jPanel1Layout.createSequentialGroup().addContainerGap(
								javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		jMenu1.setText(bundle.getString("MainWindow.jMenu1.text")); // NOI18N

		loginItem.setText("login");
		addBuddyItem.setText("addBuddy");
		deleteBuddyItem.setText("deleteBuddy");
		jMenu1.add(loginItem);
		quitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_Q,
				java.awt.event.InputEvent.ALT_MASK));
		quitItem.setText(bundle.getString("MainWindow.quitItem.text")); // NOI18N
		jMenu1.add(quitItem);

		jMenuBar1.add(jMenu1);
		jMenu2.setText("action");
		jMenu2.add(addBuddyItem);
		jMenu2.add(deleteBuddyItem);
		jMenu2.add(sayItem);
		jMenu2.setEnabled(false);
		jMenuBar1.add(jMenu2);

		jMenu3.setText(bundle.getString("MainWindow.jMenu3.text")); // NOI18N

		aboutItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_A,
				java.awt.event.InputEvent.ALT_MASK));
		aboutItem.setText(bundle.getString("MainWindow.aboutItem.text")); // NOI18N

		jMenu3.add(aboutItem);

		jMenuBar1.add(jMenu3);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(listTab,
						javax.swing.GroupLayout.Alignment.TRAILING,
						javax.swing.GroupLayout.DEFAULT_SIZE, 301,
						Short.MAX_VALUE));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jPanel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												listTab,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												343, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JList buddyList;

	private javax.swing.JTabbedPane listTab;

	private javax.swing.JList messageList;

	private JMenuItem quitItem;

	private JMenuItem aboutItem;

	// End of variables declaration

	private Core core;

	private String messageTabTitle;

	public void showIt() {
		this.setVisible(true);
		this.setExtendedState(Frame.NORMAL);
	}

}
